#include <Reg51.h>
#include "LCD1602.h"
#include "delay.h"
#include "OneWire.h"
#include "DS18B20.h"

unsigned char flag;
unsigned char code str3[] = { 0x08,0x14,0x08,0x07,0x08,0x08,0x08,0x07 }; 	//摄氏度

void Timer0Init(void);

void main()
{
	unsigned char ack;
	int temp;//保存接收温度数据
	int intTemp,decTemp;//分离温度数据的整数部分和小数部分
	unsigned char str[12];//保存数据转换成的字符串  ASCII
	unsigned char len;
	LCDInit();
	Timer0Init();
	Start18B20();
	DelayXms(10);
	while(1)
	{
		if (flag)
		{
			flag = 0;
			ack = Read18B20(&temp);
			if (ack)
			{
				intTemp = temp >> 4;//保留整数部分
				decTemp = temp & 0x0f;//保留小数部分
				len = IntegerToStr(intTemp, str);
//				str[len++] = '.';
//				
//				decTemp = (decTemp * 100) / 16;//*0.0625
//				str[len++] = decTemp / 10 + '0';//小数部分
//				str[len++] = decTemp % 10 + '0';
				while(len < 6)
				{
					str[len++] = ' ';
				}
				str[len] = '\0';
				LCDShowStr(0,0,str);
				LCDSetChar(2, 0, 0, str3);
				Start18B20();
			}
		}
	}
}

void Timer0Init(void)		//10毫秒@11.0592MHz
{
	TMOD &= 0xF0;		//设置定时器模式
	TMOD |= 0x01;		//设置定时器模式
	TL0 = 0x00;		//设置定时初值
	TH0 = 0xDC;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	ET0 = 1;
	EA = 1;
	TR0 = 1;		//定时器0开始计时
}

void Timer0_ISR() interrupt 1
{
	static unsigned char timer0Cnt = 0;
	TR0 = 0;
	TL0 = 0x00;		//设置定时初值
	TH0 = 0xDC;		//设置定时初值
	timer0Cnt++;
	if (timer0Cnt >= 100)
	{
		timer0Cnt = 0;
		flag = 1;
	}
	TR0 = 1;
}